/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.api.entityinterface.resourceinfo;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.initcolumnsstragety.BaseExtendAddPropertyInfoProcessor;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import io.iec.edp.caf.databaseobject.api.entity.DataType;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;

public class BffExtendAddPropertyInfoProcessor extends BaseExtendAddPropertyInfoProcessor {

  @Override
  public boolean useSimpleAddStatement(IGspCommonDataType commonDataType,
      IGspCommonField commonField, DataTypeInterfaceGeneratorContext parentContext) {
    GspViewModelElement viewModelElement = (GspViewModelElement) commonField;
    GspViewObject viewObject = (GspViewObject) commonDataType;
    if (beLabelIDAndPropertyNameEquals(viewObject, parentContext, viewModelElement) == false)
      return false;
    if (viewModelElement.isBigNumber() == true)
      return false;
//    if (viewModelElement.isEnableMultiLanguageInput())
//      return false;
    return true;
  }

  private boolean beLabelIDAndPropertyNameEquals(GspViewObject viewObject,
      DataTypeInterfaceGeneratorContext parentContext, GspViewModelElement viewModelElement) {
    if (viewModelElement.getIsVirtual())
      return false;
    String beMetaDataID = viewModelElement.getMapping().getTargetMetadataId();
    String beElementID = viewModelElement.getMapping().getTargetElementId();

    GspMetadata metadata = parentContext.getApiGenContext().getCefGenContext()
        .getMetadataJitContext().getMetadata(beMetaDataID);
    GspBusinessEntity beEntity = (GspBusinessEntity) metadata.getContent();

    GspBizEntityElement beElement = (GspBizEntityElement) beEntity
        .findElementById(beElementID);
    String beLabelId = beElement.getLabelID();
    return viewModelElement.getLabelID().equals(beLabelId);
  }

  @Override
  public boolean isDefaultNull(IGspCommonField field) {
    return false;
  }

  @Override
  public void addSimpleExtendPropertyArgs(List args, IGspCommonField field,
      IGspCommonDataType commonDataType, DataTypeInterfaceGeneratorContext parentContext, AST ast) {
    if (field.getMDataType() == GspElementDataType.DateTime) {
      GspViewModel viewModel = (GspViewModel) (((GspViewModelElement) field).getBelongObject()
          .getBelongModel());
      args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
    }
    if(field.getObjectType()==GspElementObjectType.Association)
    {
      GspViewModel viewModel = (GspViewModel) (((GspViewModelElement) field).getBelongObject()
              .getBelongModel());
      if(viewModel.getEnableStdTimeFormat())
      args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
    }
  }

  @Override
  public void addComplexExtendPropertyArgs(List args, IGspCommonField field,
      IGspCommonDataType commonDataType, DataTypeInterfaceGeneratorContext parentContext, AST ast) {

    GspViewModelElement viewModelElement = (GspViewModelElement) field;
    if (viewModelElement.getIsVirtual()) {
      args.add(ast.newNullLiteral());
    } else {
      String beMetaDataID = viewModelElement.getMapping().getTargetMetadataId();
      String beElementID = viewModelElement.getMapping().getTargetElementId();

      GspMetadata metadata = parentContext.getApiGenContext().getCefGenContext()
          .getMetadataJitContext().getMetadata(beMetaDataID);
      GspBusinessEntity beEntity = (GspBusinessEntity) metadata.getContent();

      GspBizEntityElement beElement = (GspBizEntityElement) beEntity
          .findElementById(beElementID);
      String beLabelId ="";
      if(beElement!=null)
       beLabelId = beElement.getLabelID();

      args.add(ExpressionUtils.getStringLiteral(ast, beLabelId));
    }
    if (field.getIsUdt()) {
      UnifiedDataTypeDef udt = (UnifiedDataTypeDef) parentContext.getApiGenContext()
          .getCefGenContext().getMetadataJitContext().getMetadata(field.getUdtID()).getContent();
      if (udt instanceof SimpleDataTypeDef) {
          SimpleDataTypeDef simpleDataTypeDef= (SimpleDataTypeDef) udt;
          if(simpleDataTypeDef.getObjectType()== GspElementObjectType.Association)
          {}
          else {
              GspViewModel viewModel = (GspViewModel) (((GspViewModelElement) field).getBelongObject()
                      .getBelongModel());
              args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
              args.add(ast.newBooleanLiteral(viewModelElement.isBigNumber()));
          }
      }
    } else if (field.getMDataType() == GspElementDataType.DateTime) {
      GspViewModel viewModel = (GspViewModel) (((GspViewModelElement) field).getBelongObject()
          .getBelongModel());
      args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
    } else if (field.getMDataType() == GspElementDataType.Decimal) {
      args.add(ast.newBooleanLiteral(viewModelElement.isBigNumber()));
    }
    if(field.getObjectType()==GspElementObjectType.Association)
    {
      GspViewModel viewModel = (GspViewModel) (((GspViewModelElement) field).getBelongObject()
              .getBelongModel());
      if(viewModel.getEnableStdTimeFormat())
      args.add(ast.newBooleanLiteral(viewModel.getEnableStdTimeFormat()));
    }
  }
}
